from flask import Blueprint,render_template,jsonify,json,request,redirect,make_response,url_for
from .models import Tickets,Order,Person
from . import db
blue = Blueprint('user',__name__)

#登陆
@blue.route('/login', methods=['GET', 'POST'])
def login():
    if request.method=='GET':
        return render_template('login.html',context=None)
    name=request.form.get('username')
    password = request.form.get('password')
    user = Person.query.filter(Person.phone_number==name).first()
    if not user:
        return render_template('login.html',message='账户不存在')
    else:
        if not user.password==password:
            return render_template('login.html', message='密码不正确')
    if user.user_type==1:

        res=make_response(redirect(url_for('user.index')))
        res.set_cookie('user_id',str(user.id),max_age=3600)
        return res
    else:
        res = make_response(redirect(url_for('user.manager')))
        res.set_cookie('user_id', str(user.id), max_age=3600)
        return res

#注册
@blue.route('/regiest', methods=['GET', 'POST'])
def regiest():
    if request.method=='GET':
        return render_template('regiest.html',context=None)
    phone=request.form.get('phone')
    name=request.form.get('name')
    password = request.form.get('password')
    user = Person.query.filter(Person.phone_number==phone).first()

    if user:

        return render_template('regiest.html',message='该手机号已经被注册')
    else:
        if not all([phone,name,password]):
            content = {
                'message': '请填写所有信息',
            }
            return render_template('regiest.html', message='请填写所有信息')
        u=Person()
        u.name=name
        u.phone_number=phone
        u.password=password
        u.user_type=1
        db.session.add(u)
        db.session.commit()

        return render_template('regiest.html', message='注册成功!')
    return render_template('regiest.html')

#退出登陆
@blue.route('/login_out', methods=['GET', 'POST'])
def login_out():
    return redirect('/user/login')

#首页
@blue.route('/index', methods=['GET', 'POST'])
def index():

    start = request.args.get('start')
    arrived = request.args.get('arrived')
    time_start = request.args.get('time_start')
    print(time_start)
    if not all([start, arrived, time_start]):
        tickets = Tickets.query.all()

        return render_template('order_system.html', tickets=tickets)
    tickets = Tickets.query.filter(Tickets.start==start).filter(Tickets.arrived==arrived).filter(Tickets.time_start.like('%'+time_start+'%')).all()
    return render_template('order_system.html', tickets=tickets)


@blue.route('/write_order', methods=['GET', 'POST'])
def write_order():
    tickets = Tickets.query.filter(Tickets.id==int(request.args.get('tk_id'))).first()
    t_id=request.args.get('tk_id')
    content = {
        'tickets': tickets,
        'ticket_id': t_id
    }

    tk_id = request.args.get('tk_id')
    name = request.args.get('name')
    phone = request.args.get('phone')
    id_card = request.args.get('id_card')
    jk = request.args.get('jk')
    num = request.args.get('num')
    time_start = request.args.get('time_start')
    arrived = request.args.get('arrived')
    print(time_start)
    if not all([tk_id, name, phone, id_card, jk]):
        return render_template('write_order.html', context=content)
    tk1 = Tickets.query.filter(Tickets.id==tk_id).first()
    num1 = tk1.seats
    tk = Tickets.query.filter(Tickets.id==tk_id).first()
    tk.seats=str(int(num1) - 1)
    db.session.add(tk)
    db.session.commit()
    order = Order()
    order.user_id=int(request.cookies.get('user_id'))
    order.ticket_id=tk_id
    order.num=num
    order.time_start=time_start
    order.arrived=arrived
    order.name=name
    order.phone=phone
    order.id_card=id_card
    order.is_safe=int(jk)
    db.session.add(order)
    db.session.commit()

    return render_template('write_order.html', context=content)

#查看订单
@blue.route('/show_order', methods=['GET', 'POST'])
def show_order():
    u_id = request.cookies.get('user_id')
    tickets = Order.query.filter(Order.user_id==u_id).all()
    return render_template('order.html', tickets=tickets)

#退票
@blue.route('/del_tk', methods=['GET', 'POST'])
def del_tk():
    tk_id = request.args.get('tk_id')
    tickets = Tickets.query.filter().all()
    order_id = request.args.get('order_id')
    content = {
        'tickets': tickets,
    }
    if not order_id:
        return redirect(url_for('user.show_order'))
    od = Order.query.filter(Order.id==order_id).first()
    db.session.delete(od)
    db.session.commit()
    num1 = Tickets.query.filter(Tickets.id==tk_id).first()
    tk = Tickets.query.filter(Tickets.id==tk_id).first()
    tk.seats=str(int(num1.seats) + 1)
    db.session.add(tk)
    db.session.commit()
    return redirect(url_for('user.show_order'))

#个人中心
@blue.route('/info', methods=['GET', 'POST'])
def info():
    u_id = request.cookies.get('user_id')
    p=Person.query.filter(Person.id==u_id).first()
    if request.method=='GET':
        return render_template('info.html',p=p)
    phone = request.form.get('phone')
    name = request.form.get('name')
    password = request.form.get('password')
    user = Person.query.filter(Person.id == u_id).first()
    user.name=name
    user.phone_number=phone
    user.password=password
    db.session.add(user)
    db.session.commit()
    return render_template('info.html',p=p)

#管理员界面
@blue.route('/manager', methods=['GET', 'POST'])
def manager():
    tickets=Tickets.query.filter().all()
    return render_template('admin.html',tickets=tickets)

#删除航班
@blue.route('/admin_s', methods=['GET', 'POST'])
def admin_s():
    order_id=request.args.get('order_id')
    tk=Tickets.query.filter(Tickets.id==order_id).first()
    db.session.delete(tk)
    db.session.commit()
    return redirect('manager')

#修改车票
@blue.route('/admin_g', methods=['GET', 'POST'])
def admin_g():
    order_id = request.args.get('order_id')
    tickets = Tickets.query.filter(Tickets.id == order_id).first()
    if request.method=='GET':

        return render_template('admin_g.html',Tickets=tickets)
    time_start=request.form.get('time_start')
    time_arrived=request.form.get('time_arrived')
    start=request.form.get('start')
    arrived=request.form.get('arrived')
    num=request.form.get('num')
    seats=request.form.get('seats')
    price=request.form.get('price')
    tickets.time_start=time_start
    tickets.time_arrived = time_arrived

    tickets.start = start
    tickets.arrived = arrived
    tickets.num = num
    tickets.seats = seats
    tickets.price = price
    db.session.add(tickets)
    db.session.commit()
    return redirect('manager')


#添加航班
@blue.route('/admin_z', methods=['GET', 'POST'])
def admin_z():
    if request.method == 'GET':
        return render_template('admin_z.html')
    time_start = request.form.get('time_start')
    time_arrived = request.form.get('time_arrived')
    start = request.form.get('start')
    arrived = request.form.get('arrived')
    num = request.form.get('num')
    seats = request.form.get('seats')
    price = request.form.get('price')
    if not all([time_start,time_arrived,start,arrived,num,seats,price]):
        return render_template('admin_z.html')
    tickets=Tickets()
    tickets.time_start = time_start
    tickets.time_arrived = time_arrived
    tickets.start = start
    tickets.arrived = arrived
    tickets.num = num
    tickets.seats = seats
    tickets.price = price
    db.session.add(tickets)
    db.session.commit()
    return redirect('manager')


#管理员个人信息
@blue.route('/admin_info', methods=['GET', 'POST'])
def admin_info():
    u_id = request.cookies.get('user_id')
    p = Person.query.filter(Person.id == u_id).first()
    if request.method == 'GET':
        return render_template('admin_info.html', p=p)
    phone = request.form.get('phone')
    name = request.form.get('name')
    password = request.form.get('password')
    user = Person.query.filter(Person.id == u_id).first()
    user.name = name
    user.phone_number = phone
    user.password = password
    db.session.add(user)
    db.session.commit()
    return render_template('admin_info.html', p=p)
